#include "head.h"
bool my_ls(sendMsg msg, client_status *user, int net_fd){
    LOG(user,&msg);
    printf("进到客户端ls处理函数\n");
    char buf[1024]={0};
    char full_path[1024]={0};
    recvMsg ret_msg;
    bzero(&ret_msg,sizeof(ret_msg));
    //获取当前真实工作目录
    getcwd(full_path,sizeof(full_path));
    //getparameter("root", full_path);
    printf("root_path:%s\n",full_path);

    /*strcat(full_path,"/");
      strcat(full_path,user.user_name);
      strcat(full_path,user.path);
      strcat(full_path,"/");
      strcat(full_path,user.path_last);
      printf("完整的路径：%s\n",full_path);
      */
    // 拼接路径
    strncat(full_path, "/", sizeof(full_path) - strlen(full_path) - 1);
    strncat(full_path, user->user_name, sizeof(full_path) - strlen(full_path) - 1);
    strncat(full_path, "/", sizeof(full_path) - strlen(full_path) - 1);
    strncat(full_path, user->path, sizeof(full_path) - strlen(full_path) - 1);
    strncat(full_path, "/", sizeof(full_path) - strlen(full_path) - 1);
    strncat(full_path, msg.msg, sizeof(full_path) - strlen(full_path) - 1);
    printf("last_path:%s\n",msg.msg);
    printf("fp：%s\n", full_path);
    //指向目录的指针
    DIR *pdir=opendir(full_path);
    if (pdir == NULL) {
        perror("opendir");
        strcpy(ret_msg.statu,"error");
        send(net_fd,&ret_msg,sizeof(buf),0);
        return false;
    } 
    //切换工作目录到指定目录
    //int ret = chdir(full_path);
    //ERROR_CHECK(ret, -1, "chdir");
    //readdir函数返回值是目录项结构体

    struct dirent *dirent;
    //循环在目录下读取目录项
    while((dirent=readdir(pdir))!=NULL){
        char *name=dirent->d_name;
        if(name[0]=='.'){
            continue;
        }//.和..是屏蔽目录项，不要了
        strcat(ret_msg.msg,name);
        
        strcat(ret_msg.msg, "/");
    };
    if(strlen(ret_msg.msg)==0){
        strcpy(ret_msg.statu,"empty");
    }   
      send(net_fd,&ret_msg,sizeof(ret_msg),0);
   
    closedir(pdir);
    return 0;
}

